package com.darren.befriend.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.darren.befriend.R;

public class ChildrenSegment extends LinearLayout implements OnClickListener {

	private Context mContext;
	private TextView mLeft;
	private TextView mRight;
	private TextView mCenter;
	private boolean mLeftSelected;
	private boolean mCenterSelected;
	private boolean mRightSelected;
	private OnChildrenChangeListener mListener;
	private static int LEFT = 0;
	private static int RIGHT = 2;
	private static int CENTER = 1;

	public ChildrenSegment(Context context) {
		super(context);
		mContext = context;
		initViews();
	}

	public ChildrenSegment(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initViews();
	}

	private void initViews() {
		LayoutInflater.from(getContext()).inflate(
				R.layout.layout_children_segment, this, true);
		mLeft = (TextView) findViewById(R.id.left);
		mLeft.setOnClickListener(this);
		mRight = (TextView) findViewById(R.id.right);
		mRight.setOnClickListener(this);
		mCenter = (TextView) findViewById(R.id.center);
		mCenter.setOnClickListener(this);
	}

	public void setOnGenderListener(OnChildrenChangeListener listener) {
		mListener = listener;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left:
			setLeftSelected(true);
			break;
		case R.id.right:
			setRightSelected(true);
			break;
		case R.id.center:
			setCenterSelected(true);
		default:
			break;
		}
	}

	/**
	 * left selected or right
	 *
	 * @return
	 */
	public int getSelected() {
		if (mLeftSelected) {
			return LEFT;
		} else if (mRightSelected) {
			return RIGHT;
		} else if (mCenterSelected) {
			return CENTER;
		}
		return 0;
	}

	/**
	 * set selected value: 1-left 2-center 3-right
	 *
	 * @param selected
	 */
	public void setSelected(int selected) {
		if (selected == LEFT) {

			setLeftSelected(true);
			setRightSelected(false);
			setCenterSelected(false);
		} else if (selected == CENTER) {

			setLeftSelected(false);
			setRightSelected(false);
			setCenterSelected(true);
		} else if (selected == RIGHT) {

			setLeftSelected(false);
			setRightSelected(true);
			setCenterSelected(false);
		}
	}

	/**
	 * set left selected and right not
	 *
	 * @param selected
	 */
	public void setLeftSelected(boolean selected) {
		mLeftSelected = selected;
		if (mListener != null && selected) {
			mListener.onChange(LEFT);
		}

		if (mLeftSelected) {

			mLeft.setTextColor(Color.WHITE);
			mLeft.setSelected(true);
			mRight.setTextColor(mContext.getResources().getColor(
					R.color.app_background_color));
			mRight.setSelected(false);
			mCenter.setTextColor(mContext.getResources().getColor(
					R.color.app_background_color));
			mCenter.setSelected(false);
		}
	}

	/**
	 * set left selected and right not
	 *
	 * @param selected
	 */
	public void setCenterSelected(boolean selected) {
		mCenterSelected = selected;
		if (mListener != null && selected) {

			mListener.onChange(CENTER);
		}

		if (mCenterSelected) {

			mCenter.setTextColor(Color.WHITE);
			mCenter.setSelected(true);
			mRight.setTextColor(mContext.getResources().getColor(
					R.color.app_background_color));
			mRight.setSelected(false);
			mLeft.setTextColor(mContext.getResources().getColor(
					R.color.app_background_color));
			mLeft.setSelected(false);
		}
	}

	/**
	 * set left selected and right not
	 *
	 * @param selected
	 */
	public void setRightSelected(boolean selected) {
		mRightSelected = selected;
		if (mListener != null && selected) {

			mListener.onChange(RIGHT);
		}

		if (mRightSelected) {

			mRight.setTextColor(Color.WHITE);
			mRight.setSelected(true);
			mCenter.setTextColor(mContext.getResources().getColor(
					R.color.app_background_color));
			mCenter.setSelected(false);
			mLeft.setTextColor(mContext.getResources().getColor(
					R.color.app_background_color));
			mLeft.setSelected(false);
		}
	}

	public interface OnChildrenChangeListener {
		public void onChange(int isSelected);
	}

}
